import {
  reactive,
  ref
} from "vue";
import {
  ElMessage,
  ElMessageBox
} from "element-plus";
import {
  parseTime
} from "@/utils";
export default function (
  props,
  emit, {
    query,
    getItems,
    getDetail,
    deleteItem
  }
) {
  const page = reactive({
    current: 1,
    size: 20,
  });

  const list = ref([]); // table list
  const total = ref(0); // total
  const checkedRows = ref([]); // table checked
  const listLoading = ref(false); // table loading
  const height = ref(0); // table height
  height.value = document.body.offsetHeight - 390;
  const currentItem = ref({}); // current item
  const editType = ref("create"); // current edit type (create update)
  let showEdit = ref(false); // show model
  const columns = ref([]); // columns
  // 重置
  const reset = () => {
    for (const key in query) {
      query[key] = "";
    }
    queryChange();
  };
  // 重画页面
  const refresh = () => {
    getList();
  };
  const queryChange = () => {
    page.current = 1;
    getList(page.current, page.size);
  };
  //  开始编辑
  const handleCreate = async (row, index) => {
    editType.value = "create";
    currentItem.value = new Object();
    showEdit.value = true;
  };
  //  开始编辑
  const handleUpdate = async (row, index) => {
    editType.value = "update";
    if (getDetail) {
      const res = await getDetail(row.id);
      currentItem.value = {
        ...res.data
      };
    } else {
      currentItem.value = {
        ...row
      };
    }
    showEdit.value = true;
  };
  // 删除
  const handleDelete = async (row) => {
    const res = await deleteItem(row.id);
    if (res.status === 200) {
      ElMessage({
        type: "success",
        message: "删除成功!",
      });
      refresh();
    } else {
      ElMessage({
        type: "warning",
        message: res.message,
      });
    }
  };
  const getList = async (pageNumber = 1, size = 20, queryData = {}) => {
    page.current = pageNumber;
    page.size = size;
    listLoading.value = true;
    const queryParams =
      Object.keys(queryData).length == 0 ? {
        ...query
      } : queryData;
    if (queryParams.daterange != undefined) {
      if (queryParams.daterange) {
        console.log(!!queryParams.daterange);
        // 结束时间 加23:59:59
        var endDate = new Date(
          new Date(queryParams.daterange[1].toLocaleDateString()).getTime() +
          24 * 60 * 60 * 1000 -
          1
        );
        queryParams.startDate = parseTime(queryParams.daterange[0]);
        queryParams.endDate = parseTime(endDate);
      } else {
        queryParams.startDate = null;
        queryParams.endDate = null;
      }
      delete queryParams.daterange;
    }
    const {
      status,
      message,
      data: {
        list: listData,
        total: totalCount
      },
    } = await getItems({
      ...queryParams,
      ...page,
    });
    listLoading.value = false;
    if (status === 200) {
      list.value = listData;
      total.value = totalCount;
    } else {
      ElMessage.warning(message);
    }
  };

  return {
    columns,
    page,
    list,
    total,
    checkedRows,
    listLoading,
    height,
    currentItem,
    editType,
    showEdit,
    reset,
    queryChange,
    refresh,
    handleCreate,
    handleUpdate,
    handleDelete,
    getList,
    reset,
  };
}